package com.huawei.petSystemdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.hms.hwid.I;
import com.huawei.petSystemdemo.bean.Adress;
import com.huawei.petSystemdemo.bean.User;
import com.huawei.petSystemdemo.utils.MyRequest;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class change_shopping_adress_Activity extends AppCompatActivity {

    TextView personName;
    TextView tel;
    Spinner province;
    Spinner homeType;
    TextView locationContent;
    private GlobalViewModel global = GlobalViewModel.getGlobal();

    TextView confirm_change;
    TextView confirm_delete;

    int addId;

    String name = "";
    String telphone = "";
    String pro = "";
    String type = "";
    String content = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_shopping_adress);


        Intent intent1 = getIntent();
         addId = intent1.getIntExtra("id", -1);




        personName = findViewById(R.id.person_name_change);
        tel = findViewById(R.id.tel_number_change);
        province = findViewById(R.id.spinner_province_change);
        homeType = findViewById(R.id.spinner_hometype_change);
        locationContent = findViewById(R.id.location_content_change);
       initinfo();//初始化修改页面的各个数据

        confirm_change = findViewById(R.id.confirm_change);
        confirm_delete = findViewById(R.id.confirm_delete);
        confirm_change.setOnClickListener(v->{
            if(ChickInput()){
                System.out.println("修改");
                Map<String,Object> requestBody = new HashMap<>();
                requestBody.put("username",personName.getText().toString());
                requestBody.put("tel",tel.getText().toString());
                requestBody.put("img",homeType.getSelectedItem().toString());
                requestBody.put("province",province.getSelectedItem().toString());
                requestBody.put("adresscontent",locationContent.getText().toString());
                requestBody.put("id",addId);
                requestBody.put("user_id", global.user.getId());
//                requestBody.put("id",id.getId());
                //requestBody.put("id")
                MyRequest.post("/shopadress/changeAdress", requestBody, new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                        String json = response.body().string();
                        System.out.println(json);
                        JSONObject jsonObject = JSON.parseObject(json);
                    }
                });
                Intent intent = new Intent(this, ShoppingAdressActivity.class);//界面跳转
                startActivity(intent);//
            }

        });


        confirm_delete.setOnClickListener(v->{

                System.out.println("删除");
                Map<String,Object> requestBody = new HashMap<>();
                requestBody.put("id",addId);
//                requestBody.put("id",id.getId());
                //requestBody.put("id")
                MyRequest.post("/shopadress/deleteAdress", requestBody, new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                        String json = response.body().string();
                        System.out.println(json);
                        JSONObject jsonObject = JSON.parseObject(json);
                    }
                });
                Intent intent = new Intent(this, ShoppingAdressActivity.class);//界面跳转
                startActivity(intent);//


        });
    }





    public boolean ChickInput(){
        String StuNumber = personName.getText().toString();
        String StuPwd = tel.getText().toString();
        //trim() 方法用于删除字符串的头尾空白符
        if(StuNumber.trim().equals("")) {
            Toast.makeText(change_shopping_adress_Activity.this,"收货人不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(StuPwd.trim().equals("")) {
            Toast.makeText(change_shopping_adress_Activity.this,"电话不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    public void onClick (android.view.View back){
        Intent intent = new Intent(this, ShoppingAdressActivity.class);//界面跳转
        startActivity(intent);//

    }






    public void initinfo(){

        Intent intent = getIntent();
        addId = intent.getIntExtra("id", -1);

        Map<String,Object> requestBody = new HashMap<>();
        requestBody.put("addId",addId);
        MyRequest.get("/shopadress/getAddById", requestBody, json -> {
            JSONObject jsonObject = JSON.parseObject(json);
            System.out.println(jsonObject);
            if(jsonObject != null){
                Adress adress = jsonObject.getObject("data", Adress.class);//可以把data转化成user对象
                System.out.println(adress);
                name = adress.getUserName();
                telphone=adress.getTel();
                content=adress.getAdressContent();

                //String tel = jsonObject.getString("tel");
                System.out.println(name + telphone + content);
                //Toast.makeText(Personal_page.this,"信息修改成功",Toast.LENGTH_SHORT).show();
                System.out.println("信息修改成功");
                //Looper.prepare();
                personName.setText(name);
                tel.setText(telphone);
                locationContent.setText(content);
            }else {
                System.out.println("getByUsername 获取json失败");
            }
        });



    }
}
